<?php

use App\Admin\Controller\Base\AdministratorsController;
use App\Common\Enums\Base\AdministratorsEnumStatus;
use App\Common\Model\Base\Administrators;
use App\Common\Model\Base\Role;
use App\Common\Util\TP;
use Sc\Util\HtmlStructure\Form;
use Sc\Util\HtmlStructure\Form\FormItem;
use Sc\Util\HtmlStructure\Html\Html;

Html::create('添加管理员');

$form = Form::create('administrators')->config('label-width', '80px');

$form->addFormItems(
    FormItem::hidden('id', ''),
    FormItem::text('name', '用户名'),
    FormItem::text('account', '账号'),
    FormItem::password('password', '密码')->setWhen('!administrators.id'),
    FormItem::radio('status', '状态')->options(AdministratorsEnumStatus::mapping()),
    FormItem::select('role_ids', '角色')->multiple()->options(Role::getSelectData('name')),
    FormItem::submit()->successClose('current')->submitUrl(
        TP::route()->to([AdministratorsController::class, 'store'])->url(),
        TP::route()->to([AdministratorsController::class, 'update'])->url(),
    )->success('VueApp.administratorsGetData()')
);

if(TP::request()->get('id')){
    $data = Administrators::find(TP::request()->get('id'))?->toArray() ?: [];
    $form->setData($data);
}
return $form->render();